from rest_framework.decorators import api_view
from rest_framework.response import Response
from student.models import StudentData
from ocr.card.core import get_sfz_id

@api_view(["POST"])
def sfz_ocr(request):
    sfz = request.data.get("sfz",None)
    if not sfz:
        return Response({
            "result":False,
            "message":"请拍摄身份证",
            "code":40001
        })
    success,data = get_sfz_id(sfz)
    if success:
        sfzid = data["sfzid"]
        name = data["name"]
        stu = StudentData.objects.filter(sfzid=sfzid).first()
        if not stu:
            return Response({
                "result":False,
                "message":"请先导入学生信息",
                "code":40002
            })
        stu.sfz_img = sfz
        stu.save()
        return Response({
            "result":success,
            "message":"ok",
            "data":data
        })
    return Response({
        "result":False,
        "message":data,
        "code":40002
    })
    

